package decorator;

// 抽象组件：定义咖啡的基本功能
interface Coffee {
    double cost();          // 计算价格

    String description();   // 获取描述
}

// 具体组件：普通咖啡
class SimpleCoffee implements Coffee {
    @Override
    public double cost() {
        return 8.0; // 基础咖啡价格
    }

    @Override
    public String description() {
        return "Simple Coffee";
    }
}

// 装饰器：持有一个 Coffee 对象，并继承 Coffee
abstract class CoffeeDecorator implements Coffee {
    protected Coffee coffee;

    public CoffeeDecorator(Coffee coffee) {
        this.coffee = coffee;
    }

    @Override
    public double cost() {
        return coffee.cost();
    }

    @Override
    public String description() {
        return coffee.description();
    }
}

// 具体装饰器：加牛奶
class MilkDecorator extends CoffeeDecorator {
    public MilkDecorator(Coffee coffee) {
        super(coffee);
    }

    @Override
    public double cost() {
        return super.cost() + 2.0; // 牛奶额外加 $2.0
    }

    @Override
    public String description() {
        return super.description() + ", Milk";
    }
}

// 具体装饰器：加糖
class SugarDecorator extends CoffeeDecorator {
    public SugarDecorator(Coffee coffee) {
        super(coffee);
    }

    @Override
    public double cost() {
        return super.cost() + 1.0; // 糖额外加 $1.0
    }

    @Override
    public String description() {
        return super.description() + ", Sugar";
    }
}

public class Consumer {
    public static void main(String[] args) {
        // 先点一杯普通咖啡
        Coffee coffee = new SimpleCoffee();
        System.out.println(coffee.description() + " -> $" + coffee.cost());
        // Simple Coffee -> $8.0

        // 加牛奶
        coffee = new MilkDecorator(coffee);
        System.out.println(coffee.description() + " -> $" + coffee.cost());
        // Simple Coffee, Milk -> $10.0

        // 加糖
        coffee = new SugarDecorator(coffee);
        System.out.println(coffee.description() + " -> $" + coffee.cost());
        // Simple Coffee, Milk, Sugar -> $11.0
    }
}